#include "SnatchFragmentIcon.h"
#include "SnatchFragmentIconLoader.h"
#include "../../model/proxy/ItemProxy.h"

SnatchFragmentIcon::SnatchFragmentIcon(void)
	:mCount(NULL)
{
}

SnatchFragmentIcon::~SnatchFragmentIcon(void)
{
	CC_SAFE_RELEASE(mCount);
}

bool SnatchFragmentIcon::onAssignCCBMemberVariable( CCObject * pTarget, CCString * pMemberVariableName, CCNode * pNode )
{
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mCount", CCLabelTTF *, this->mCount);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mMask", CCSprite *, this->mMask);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mCountBg", CCSprite *, this->mCountBg);

	return false;
}

SEL_MenuHandler SnatchFragmentIcon::onResolveCCBCCMenuItemSelector(CCObject * pTarget, CCString * pSelectorName )
{
	CCB_SELECTORRESOLVER_CCMENUITEM_GLUE(this, "onDoItClick", SnatchFragmentIcon::onDoItClick);
	return NULL;
}

SEL_CCControlHandler SnatchFragmentIcon::onResolveCCBCCControlSelector( CCObject * pTarget, CCString * pSelectorName )
{
	CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "onMergeBtnClick", SnatchFragmentIcon::onMergeBtnClick);
	CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "onCloseBtnClick", SnatchFragmentIcon::onCloseBtnClick);
	return NULL;
}

void SnatchFragmentIcon::onNodeLoaded( CCNode * pNode, CCNodeLoader * pNodeLoader )
{
	mItemIcon = Item::create();
	mItemIcon->setPosition(ccp(47, 45));
	mItemIcon->setScale(0.3);
	addChild(mItemIcon);
}

void SnatchFragmentIcon::onEnter()
{
	CCNode::onEnter();
}

void SnatchFragmentIcon::onExit()
{
	CCNode::onExit();
}

bool SnatchFragmentIcon::init()
{
	return true;
}

void SnatchFragmentIcon::refresh()
{
	int itemID = int(mData);
	int itemCount = ItemProxy::getSingletonPtr()->getItemCount(itemID);
	mItemIcon->itemID = itemID;
	mItemIcon->setIcon();
	if (itemCount > 0)
	{
		this->addChild(mCount);
		this->addChild(mCountBg);
		mCount->setString(fcs("%d", itemCount));
		mMask->retain();
		mMask->removeAllChildrenWithCleanup(false);
	}
	else
	{
		mCount->retain();
		mCount->removeAllChildrenWithCleanup(false);
		mCountBg->retain();
		mCountBg->removeAllChildrenWithCleanup(false);
		this->addChild(mMask);
	}
}

void SnatchFragmentIcon::_onNotification( CCObject* object )
{
	
}

void SnatchFragmentIcon::onDoItClick(CCObject* pSender)
{
    
}

void SnatchFragmentIcon::onMergeBtnClick( CCObject * pSender, CCControlEvent pCCControlEvent )
{

}

void SnatchFragmentIcon::onCloseBtnClick( CCObject * pSender, CCControlEvent pCCControlEvent )
{

}

SnatchFragmentIcon* SnatchFragmentIcon::create( CCObject* pOwer )
{
	RegisterLoader("SnatchFragmentIcon", SnatchFragmentIconLoader::loader());
	return (SnatchFragmentIcon*)(ReadNode("SnatchFragmentIcon.ccbi", pOwer));
}
